package eteam.aps.service.impl;

import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.ProtectionOrgDtlMapper;
import eteam.aps.mapper.ProtectionOrgMapper;
import eteam.aps.model.dto.ProtectionOrgDto;
import eteam.aps.model.entity.ProtectionOrg;
import eteam.aps.model.vo.ProtectionOrgListVo;
import eteam.aps.model.vo.ProtectionOrgVo;
import eteam.aps.model.vo.ProtectionVo;
import eteam.aps.service.ProtectionOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 保障机构业务层
 *
 * @author WMT
 */
@Service
public class ProtectionOrgServiceImpl implements ProtectionOrgService {

    @Autowired
    private ProtectionOrgMapper protectionOrgMapper;

    @Autowired
    private ProtectionOrgDtlMapper protectionOrgDtlMapper;

    @Transactional
    @Override
    public int insert(ProtectionOrgDto protectionOrgDto) {
        return protectionOrgMapper.insert(protectionOrgDto);
    }

    @Transactional
    @Override
    public int delete(String fId) {
        return protectionOrgMapper.deleteByFId(fId);
    }

    @Transactional
    @Override
    public int update(ProtectionOrgDto protectionOrgDto) {
        return protectionOrgMapper.updateOne(protectionOrgDto);
    }

    @Override
    public JsonPage<ProtectionOrgListVo> selectAll(Map<String, Object> map, int page, int pageSize) {
        map.put("fState",ParentEntity.FSTATE_DELETED);
        int totalCount = protectionOrgMapper.selectCountAll(map);
        List<ProtectionOrgListVo> list = null;
        if (totalCount > 0){
            map.put("offset", (page - 1) * pageSize);
            map.put("limit", pageSize);
            list = protectionOrgMapper.selectAll(map);
        }
        return JsonPage.builder(page,pageSize,totalCount,list);
    }

    @Override
    public List<ProtectionOrgVo> selectDetails(String fId) {
        List<ProtectionOrgVo> protectionOrgVos = protectionOrgMapper.selectDetails(fId);
        Map<String,Object> map = new HashMap<>(2);
        map.put("fDtlId",fId);
        map.put("fState",ParentEntity.FSTATE_DELETED);
        for (ProtectionOrgVo vo : protectionOrgVos) {
            vo.setOrgDtlList(protectionOrgDtlMapper.selectAll(map));
        }

        return protectionOrgVos;
    }


    @Override
    public int selectCountByCode(String orgCode) {
        return protectionOrgMapper.selectCountByCode(orgCode);
    }

    @Override
    public int selectCountByName(String orgName) {
        return protectionOrgMapper.selectCountByName(orgName);
    }

    @Override
    public ProtectionOrg selectByFId(String fId) {
        return protectionOrgMapper.selectByFId(fId);
    }

    @Override
    public int selectFStateByFId(String fId) {
        return protectionOrgMapper.selectFStateByFId(fId);
    }

    @Override
    public List<ProtectionVo> selectProOrgList() {
        return protectionOrgMapper.selectProOrgList(ParentEntity.FSTATE_DELETED);
    }
}
